package com.rjs.serialization;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

public class HessianSerializer implements ISerializer {

	public byte[] serialize(Object obj) throws IOException {
		if (obj == null) {
			throw new NullPointerException();
		}
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		HessianOutput hessianOutput = new HessianOutput(byteArrayOutputStream);
		hessianOutput.writeObject(obj);
		hessianOutput.close();
		return byteArrayOutputStream.toByteArray();
	}

	@SuppressWarnings("unchecked")
	public <T> T deserialize(byte[] buf, Class<T> clzz) throws IOException, ClassNotFoundException {
		if (buf == null) {
			throw new NullPointerException();
		}
		HessianInput hessianInput = new HessianInput(new ByteArrayInputStream(buf));
		return (T)hessianInput.readObject(clzz);
	}

}
